/*
	libsimple_graphical - unambitious C++ graphical library
	https://notabug.org/namark/libsimple_graphical
	https://gitlab.com/namark/libsimple_graphical
	https://github.com/namark/libsimple_graphical

	Copyright (C) 2017-2019 respective authors/contributors

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#include "simple/sdlcore/utils.hpp"
#include "algorithm.h"
#include "surface.h"
#include "renderer.h"

namespace simple::graphical
{

	bool fill(const surface& surf, color col)
	{
		return !sdlcore::utils::check_error(
			SDL_FillRect(surf.guts().get(), NULL, static_cast<uint32_t>(col)));
	}

	bool fill(const surface& surf, color col, const range2D& area)
	{
		auto r = sdlcore::utils::to_rect<SDL_Rect>(area);
		return !sdlcore::utils::check_error(
			SDL_FillRect(surf.guts().get(), &r, static_cast<uint32_t>(col)));
	}

	bool fill(const renderer& rend)
	{
		return !sdlcore::utils::check_error(SDL_RenderClear(rend.guts().get()));
	}

	bool fill(const renderer& rend, const range2D& area)
	{
		auto r = sdlcore::utils::to_rect<SDL_Rect>(area);
		return !sdlcore::utils::check_error(SDL_RenderFillRect(rend.guts().get(), &r));
	}

	bool fill(const renderer& rend, const rgba_pixel& color)
	{
		rend.color(color);
		return fill(rend);
	}

	bool fill(const renderer& rend, const rgba_pixel& color, const range2D& area)
	{
		rend.color(color);
		return fill(rend, area);
	}

	bool blit(const surface& source, const surface& destination, int2 position)
	{
		return blit(source, rect{ source.size() }, destination, position);
	}

	bool blit(const surface& source, range2D src_range, const surface& destination, int2 position)
	{
		auto sr = sdlcore::utils::to_rect<SDL_Rect>(src_range);
		auto dr = sdlcore::utils::to_rect<SDL_Rect>({position});
		return !sdlcore::utils::check_error(
			SDL_BlitSurface(source.guts().get(), &sr, destination.guts().get(), &dr));
	}

	bool blit(const surface& source, const surface& destination, range2D dest_range)
	{
		return blit(source, rect{ source.size() }, destination, dest_range);
	}

	bool blit(const surface& source, range2D src_range, const surface& destination, range2D dest_range)
	{
		auto sr = sdlcore::utils::to_rect<SDL_Rect>(src_range);
		auto dr = sdlcore::utils::to_rect<SDL_Rect>(dest_range);
		return !sdlcore::utils::check_error(
			SDL_BlitScaled(source.guts().get(), &sr, destination.guts().get(), &dr));
	}

	surface convert(const surface& source, const pixel_format& format)
	{
		return surface( SDL_ConvertSurface(source.guts().get(), format.guts().get(), 0) );
	}

} // namespace simple::graphical
